//
//  DDTabBarController.m
//  地表最帅
//
//  Created by Mr.Leesin on 17/1/7.
//  Copyright © 2017年 weiLaiCompany. All rights reserved.
//

#import "DDTabBarController.h"
#import "DDHomeController.h"
@interface DDTabBarController ()

@end

@implementation DDTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self addChildViewControllers];
}
- (void)addChildViewControllers {
    
    // 设置 tabBar 的字体颜色
    // self.tabBar.tintColor = [UIColor cz_colorWithHex:0xDF0000];
    
    // 视图控制器的字典数组
    NSArray *array = @[
                       @{@"clsName": @"DDHomeController", @"title": @"首页", @"imageName": @"tab_normal_1"},
                       @{@"clsName": @"UIViewController", @"title": @"发现", @"imageName": @"tab_normal_2"},
                       @{@"clsName": @"UIViewController", @"title": @"健康", @"imageName": @"tab_normal_3"},
                       ];
    
    // 遍历数组，创建控制器数组
    NSMutableArray *arrayM = [NSMutableArray array];
    for (NSDictionary *dict in array) {
        
        [arrayM addObject:[self childControllerWithDict:dict]];
    }
    
    // 设置子控制器数组
    self.viewControllers = arrayM.copy;
}

/**
 * 创建一个子控制器
 */
- (UIViewController *)childControllerWithDict:(NSDictionary *)dict {
    
    // 1. 创建控制器
    NSString *clsName = dict[@"clsName"];
    Class cls = NSClassFromString(clsName);
    
    NSAssert(cls != nil, @"传入了类名错误");
    UIViewController *vc = [cls new];
    
    // 2. 设置标题
    vc.title = dict[@"title"];
    
    // 3. 图片
    vc.tabBarItem.image = [UIImage imageNamed:dict[@"imageName"]];
    
    // 高亮图片
    NSString *imageNameHL = [NSString stringWithFormat:@"%@_selected", dict[@"imageName"]];
    vc.tabBarItem.selectedImage = [[UIImage imageNamed:imageNameHL] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    // 4. 添加导航控制器
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
    
    // 5. 返回导航控制器
    return nav;
}

@end
